/*
 * Copyright (C) 2009  Sylvain Maucourt (smaucourt@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *
 */
package net.sylvek.where;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.TimerTask;

public class FindFriends extends TimerTask {

    static final int DONE = 10000;

    static final int EMPTY = 0;

    private final Handler progress;

    private final long[] friends;

    FindFriends(Handler progress, long[] friends)
    {
        this.progress = progress;
        this.friends = friends;
    }

    public void run()
    {

        try {
            progress.sendEmptyMessage(EMPTY);

            if (friends == null || friends.length == 0) {
                return;
            }

            JSONArray result = SylvekClient.getFriends(friends);
            int nbFriends = result.length();

            if (nbFriends == 0) {
                return;
            }

            int step = DONE / nbFriends;
            int currentStep = EMPTY + 1;

            for (int i = 0; i < nbFriends; i++) {
                JSONObject friend = result.getJSONObject(i);
                double latitude = friend.getDouble(FriendOverlay.LATITUDE);
                double longitude = friend.getDouble(FriendOverlay.LONGITUDE);
                String name = friend.getString(FriendOverlay.NAME);
                int uid = friend.getInt(FriendOverlay.UID);
                boolean isOnline = friend.getBoolean(FriendOverlay.ONLINE);
                String update = friend.getString(FriendOverlay.SORTING);

                Message msg = Message.obtain();
                Bundle bundle = new Bundle();
                bundle.putBoolean(FriendOverlay.ONLINE, isOnline);
                bundle.putString(FriendOverlay.NAME, name);
                bundle.putInt(FriendOverlay.UID, uid);
                bundle.putDouble(FriendOverlay.LATITUDE, latitude);
                bundle.putDouble(FriendOverlay.LONGITUDE, longitude);
                bundle.putString(FriendOverlay.SORTING, update);
                msg.setData(bundle);

                currentStep += step * i;
                msg.what = currentStep;
                progress.sendMessage(msg);
            }

        } catch (Exception e) {
            Log.e("found.friends", e.getMessage());
        } finally {
            progress.sendEmptyMessage(DONE);
        }
    }
}